//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IMM.h"
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Times::CIMM::CIMM (boost::shared_ptr<QuantLib::IMM>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIMM = NULL;
#endif
	_ppIMM = &childNative;
    
}
Cephei::QL::Times::CIMM::CIMM (QuantLib::IMM& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIMM = NULL;
#endif
	_ppIMM = new boost::shared_ptr<QuantLib::IMM> (&childNative);
    
    _IMMOwner = owner;
    
}

Cephei::QL::Times::CIMM::CIMM (CIMM^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIMM = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIMM = new boost::shared_ptr<QuantLib::IMM> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CIMM::CIMM (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIMM = NULL;
#endif
	if (!t->IsSubclassOf(CIMM::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CIMM::CIMM (QuantLib::Handle<QuantLib::IMM>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIMM = &childNative;
	_ppIMM = &static_cast<boost::shared_ptr<QuantLib::IMM>>(childNative.currentLink());
    
    _IMMOwner = owner;
}
Cephei::QL::Times::CIMM::CIMM (QuantLib::Handle<QuantLib::IMM> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIMM = &childNative;
	_ppIMM = &static_cast<boost::shared_ptr<QuantLib::IMM>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CIMM::CIMM (QuantLib::IMM childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIMM = NULL;
#endif
	_ppIMM = new boost::shared_ptr<QuantLib::IMM> (new QuantLib::IMM (childNative));
    
}
#endif

Cephei::QL::Times::CIMM::~CIMM ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIMM != NULL)
    {
	    delete _ppIMM;
        _ppIMM = NULL;
    }
}
Cephei::QL::Times::CIMM::!CIMM ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIMM != NULL)
    {
	    delete _ppIMM;
    }
}
QuantLib::IMM& Cephei::QL::Times::CIMM::GetReference ()
{
    if (_ppIMM == NULL) throw gcnew NativeNullException ();
	return **_ppIMM;
}
boost::shared_ptr<QuantLib::IMM>& Cephei::QL::Times::CIMM::GetShared ()
{
    if (_ppIMM == NULL) throw gcnew NativeNullException ();
	return *_ppIMM;
}
QuantLib::IMM* Cephei::QL::Times::CIMM::GetPointer ()
{
    if (_ppIMM == NULL) throw gcnew NativeNullException ();
	return &**_ppIMM;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IMM>& Cephei::QL::Times::CIMM::GetHandle ()
{
	if (_phIMM == NULL)
	{
		_phIMM = new Handle<QuantLib::IMM> (*_ppIMM);
	}
	return *_phIMM;
}
#endif
bool Cephei::QL::Times::CIMM::HasNative () 
{
	return (_ppIMM != NULL);
}

String^ Cephei::QL::Times::CIMM::Code (DateTime immDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _immDate = (QuantLib::Date)ValueHelper::Convert (immDate);
    	std::string _rv = (std::string)(*_ppIMM)->code ( _immDate );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CIMM::Date (String^ immCode, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _immCode = (std::string)ValueHelper::Convert (immCode);
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIMM)->date ( _immCode,  _referenceDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CIMM::IsIMMcode (String^ p_in, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _p_in = (std::string)ValueHelper::Convert (p_in);
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
    	bool _rv = (bool)(*_ppIMM)->isIMMcode ( _p_in,  _mainCycle );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CIMM::IsIMMdate (DateTime d, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
    	bool _rv = (bool)(*_ppIMM)->isIMMdate ( _d,  _mainCycle );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CIMM::NextCode (String^ immCode, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _immCode = (std::string)ValueHelper::Convert (immCode);
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	std::string _rv = (std::string)(*_ppIMM)->nextCode ( _immCode,  _mainCycle,  _referenceDate );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CIMM::NextCode (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
    	std::string _rv = (std::string)(*_ppIMM)->nextCode ( _d,  _mainCycle );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CIMM::NextDate (String^ immCode, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _immCode = (std::string)ValueHelper::Convert (immCode);
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIMM)->nextDate ( _immCode,  _mainCycle,  _referenceDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CIMM::NextDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d, Microsoft::FSharp::Core::FSharpOption<Boolean>^ mainCycle)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
        bool _mainCycle = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (mainCycle) ? (bool)ValueHelper::Convert (mainCycle->Value) : true); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIMM)->nextDate ( _d,  _mainCycle );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

